<?php
include '../../connect.php';
$tungay=isset($_REQUEST['tungay'])?$_REQUEST['tungay']:'';
$dengay=isset($_REQUEST['denngay'])?$_REQUEST['denngay']:'';
$masan=isset($_REQUEST['masan'])?$_REQUEST['masan']:'';
$wh='';$stt=0;
$tongtienthue=0;$tongthuesanmoi=0;$tongthuesandatcoc=0;$tongthuesanhoantat=0;$tongthuesanhuy=0;
$tongtienthanhtoan=0;$tongtienttsandatcoc=0;$tongtienttsanhoantat=0;
$tongtienconlai=0;$tongtienconlaisandatcoc=0;
if($masan!='')
	$wh=" and p.SanID='$masan'";
	
	$query="select * from phieudatsan p INNER JOIN lichdatsan l
			ON p.PhieuDatSanID=l.PhieuDatSanID
			where 1 $wh and p.NgayLap BETWEEN '$tungay' AND '$dengay'
			ORDER BY p.TrangThai ASC";
	$result = mysql_query($query);
	$numthongke=mysql_num_rows($result);
	if($numthongke<=0)
		return;
?>	

<div id="tinhtrangsan">
<span style="border:#000 thin solid;padding:0 20px 0 20px;">&nbsp;</span> Sân chưa đặt cọc
<span style="border:#000 thin solid;padding:0 20px 0 20px; background:#FFC">&nbsp;</span> Sân đã đặt cọc
<span style="border:#000 thin solid;padding:0 20px 0 20px; background:#A893FF">&nbsp;</span> Sân đã thanh toán
<span style="border:#000 thin solid;padding:0 20px 0 20px; background:#F00">&nbsp;</span> Sân đã Hủy
</div>
<table border="0" width="100%" cellpadding="0" cellspacing="0" height="50px">
				<tr>
					<th class="table-header-repeat line-left"  width="50px"><a href="">STT</a>	</th>
					<th class="table-header-repeat line-left" width="210px"><a href="">Mã Phiếu</a></th>
                    <th class="table-header-repeat line-left" width="210px"><a href="">Sân</a></th>
					<th class="table-header-repeat line-left"  width="210px"><a href="">Tiền Thuê</a></th>
					<th class="table-header-repeat line-left"  width="210px"><a href="">Tiền Đã Thanh Toán</a></th>
                    <th class="table-header-repeat line-left" width="180px"><a href="">Tiền Còn Lại</a></th>
				</tr>
</table>

<?php
echo "<table border='0' width='100%' cellpadding='0' cellspacing='0' id='product-table'>";
	while($row=mysql_fetch_array($result))
	{
		 $maphieu=$row['PhieuDatSanNo'];
		 $sanid=$row['SanID'];
		 $thanhtien=$row['ThanhTien'];
		 $tienthanhtoan=0;
		 $tienconlai=0;
		 $trangthai=$row['TrangThai'];
		 $stt++;
		 if($trangthai==1)
		 {
				echo "<tr style=\"background:#FFC;\">";
				$tienthanhtoan=$thanhtien*0.5;
				$tienconlai=$thanhtien-$tienthanhtoan;
				$tongthuesandatcoc+=$thanhtien;
				$tongtienttsandatcoc+=$tienthanhtoan;
				$tongtienconlaisandatcoc+=$tienconlai;
		 }
		 else if($trangthai==2)
		 {
				echo "<tr style=\"background:#A893FF;\">";
				$tienthanhtoan=$thanhtien;
				$tongthuesanhoantat+=$thanhtien;
				$tongtienttsanhoantat+=$tienthanhtoan;
		 }
		 else if($trangthai==3)
		 {
				echo "<tr style=\"background:red;\" >";
				$tienconlai=$thanhtien;
				$tongthuesanhuy+=$thanhtien;
		 }
		 else
		 {
				echo "<tr style=\"background:white;\">";
				$tienconlai=$thanhtien;
				$tongthuesanmoi+=$thanhtien;
		 }
		 echo " <td width='55px'>$stt</td>
                <td width='232px'>$maphieu</td>
                <td width='233px'>$sanid</td>
                <td  width='233px'>$thanhtien</td>
                <td width='232px'>$tienthanhtoan</td>
                <td width='200px'>$tienconlai</td>
               ";
		 $tongtienconlai+=$tienconlai;
		 $tongtienthanhtoan+=$tienthanhtoan;
		 $tongtienthue+=$thanhtien;
	}
?>
</tr>
</table>
<br />
<?php
	echo "Tổng Tiền Thuê : $tongtienthue VNĐ<br/>";
	echo "==>Sân Mới : $tongthuesanmoi VNĐ<br/>";
	echo "==>Sân Đặt Cọc : $tongthuesandatcoc VNĐ<br/>";
	echo "==>Sân Đã Thanh Toán : $tongthuesanhoantat VNĐ<br/>";
	echo "==>Sân Đã Hủy : $tongthuesanhuy VNĐ<br/><br/>";
	
	
	echo "Tổng Tiền Thanh Toán : $tongtienthanhtoan VNĐ<br/>";
	echo "==>Sân Mới : 0 VNĐ<br/>";
	echo "==>Sân Đặt Cọc : $tongtienttsandatcoc VNĐ<br/>";
	echo "==>Sân Đã Thanh Toán : $tongtienttsanhoantat VNĐ<br/>";
	echo "==>Sân Đã Hủy : 0 VNĐ<br/><br/>";
	
	
	echo "Tổng Tiền Còn Lại : $tongtienconlai VNĐ<br/>";
	echo "==>Sân Mới : $tongthuesanmoi VNĐ<br/>";
	echo "==>Sân Đặt Cọc : $tongtienconlaisandatcoc VNĐ<br/>";
	echo "==>Sân Đã Thanh Toán : 0 VNĐ<br/>";
	echo "==>Sân Đã Hủy : $tongthuesanhuy VNĐ<br/><br/>";
	
	$querysan="select count(p.TrangThai) as SoSan,p.SanID,p.TrangThai from phieudatsan p INNER JOIN lichdatsan l
			ON p.PhieuDatSanID=l.PhieuDatSanID
			where 1 $wh and p.NgayLap BETWEEN '$tungay' AND '$dengay'
			GROUP BY p.TrangThai ";
	$resultsan = mysql_query($querysan);
	$moi=0;$datsan=0;$dathanhtoan=0;$huy=0;
	while($rowsan=mysql_fetch_array($resultsan))
	{
		$sosan=$rowsan["SoSan"];
		$trangthaisan=$rowsan["TrangThai"];
		if($trangthaisan==0)
		$moi=$sosan;
		if($trangthaisan==1)
		$datsan=$sosan;
		if($trangthaisan==2)
		$dathanhtoan=$sosan;
		if($trangthaisan==3)
		$huy=$sosan;
	}
	echo "Tổng Số Sân Mới : $moi <br/>";
	echo "Tổng Số Sân Đã Đặt Cọc : $datsan <br/>";
	echo "Tổng Số Sân Đã Thanh Toán : $dathanhtoan <br/>";
	echo "Tổng Số Sân Đã Hủy : $huy <br/>";
?> 
